package com.tg.data.http.entity;

import android.os.Parcel;
import android.os.Parcelable;
import java.util.ArrayList;
import java.util.List;
import io.objectbox.annotation.Entity;
import io.objectbox.annotation.Id;
import io.objectbox.annotation.Transient;

@Entity
public class GetOsdRespBean implements Parcelable {
    public static final int NONE_OR_CLOSE = -1;
    public static final int OSDT_TEXT_LENGTH = 32;

    @Id
    public   long id;//设备ID

    public  String uuid;//设备UUID
    public int fSupportedTypes;
    public int eCharEncoding;
    public int nMaxTextLength = OSDT_TEXT_LENGTH; // OSDT_TEXT 允许的最大字节数. 如果为0的话，默认为32字节
    public int nMaxOsdItems;
    public int nItems;
    public int open;
    public int txtIndex;
    public String text;
    @Transient
    public List<OsdItem> items = new ArrayList<>();
    public GetOsdRespBean() {}

    protected GetOsdRespBean(Parcel in) {
        id = in.readLong();
        uuid = in.readString();
        fSupportedTypes = in.readInt();
        eCharEncoding = in.readInt();
        nMaxTextLength = in.readInt();
        nMaxOsdItems = in.readInt();
        nItems = in.readInt();
        open = in.readInt();
        txtIndex = in.readInt();
        text = in.readString();
        items = in.createTypedArrayList(OsdItem.CREATOR);
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(id);
        dest.writeString(uuid);
        dest.writeInt(fSupportedTypes);
        dest.writeInt(eCharEncoding);
        dest.writeInt(nMaxTextLength);
        dest.writeInt(nMaxOsdItems);
        dest.writeInt(nItems);
        dest.writeInt(open);
        dest.writeInt(txtIndex);
        dest.writeString(text);
        dest.writeTypedList(items);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<GetOsdRespBean> CREATOR = new Creator<GetOsdRespBean>() {
        @Override
        public GetOsdRespBean createFromParcel(Parcel in) {
            return new GetOsdRespBean(in);
        }

        @Override
        public GetOsdRespBean[] newArray(int size) {
            return new GetOsdRespBean[size];
        }
    };

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }


    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }


    public short getUniqueItemId(){
        short uniqueId = OsdItem.OSD_MIN_ID -  1;
        for (OsdItem osdItem : this.items){
            if (osdItem.itemId > uniqueId){
                uniqueId = osdItem.itemId;
            }
        }
        if (uniqueId == OsdItem.OSD_MIN_ID -  1){
            return OsdItem.OSD_MIN_ID;
        }

        byte[] result = new byte[uniqueId + 2];
        for (int i = 0; i < uniqueId + 2; i++){
            result[i] = 0;
        }
        for (OsdItem osdItem : this.items){
            result[osdItem.itemId] = 1;
        }

        for (int i = 0; i < uniqueId + 2; i++){
            if (result[i] == 0){
                uniqueId = (short)i;
                break;
            }
        }

        return uniqueId;
    }


}
